package com.action;

import com.config.WebConfig;
import com.hdb.DB;
import com.hdb.TAdmin;
import com.hdb.TUser;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.javafx.fxml.builder.URLBuilder;
import com.sun.jndi.toolkit.url.Uri;
import com.util.Cart;
import jdk.nashorn.internal.runtime.URIUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.*;
import java.nio.file.Files;
import java.util.Date;
import java.util.Map;

/**
 * Created by aszer on 2016/11/9.
 *
 */
class BasicAction extends ActionSupport {
    DB db;
    File upload;
    String uploadFileName;
    int page;
    String message;
    String path;
    /*获得session对象*/
    HttpSession getSession() {
        return ServletActionContext.getRequest().getSession();
    }
    /*获得request的Map对象,用于向JSP页面传递数据*/
    Map getRequest() {
        return (Map) ServletActionContext.getContext().get("request");
    }
    /*从session中获得TUser对象,不存在返回null*/
    TUser getUser() {
        return (TUser) getSession().getAttribute("user");
    }

    TAdmin getAdmin() {
        return (TAdmin) getSession().getAttribute("admin");
    }
    /*设置session中的TUser对象,传递null时从session中删除*/
    void setUser(TUser user) {
        if (user == null)
            getSession().removeAttribute("user");
        else
            getSession().setAttribute("user", user);
    }
    /*类似setUser*/
    void setAdmin(TAdmin admin) {
        if (admin == null)
            getSession().removeAttribute("admin");
        else
            getSession().setAttribute("admin", admin);
    }

    String getResultInfo(Boolean res) {
        return res ? "操作成功" : "操作失败";
    }

    void setCart(Cart cart) {
        if (cart == null)
            getSession().removeAttribute("cart");
        else
            getSession().setAttribute("cart", cart);
    }

    Cart getCart() {
        return (Cart) getSession().getAttribute("cart");
    }

    /*对上传的图片进行处理,修改名称并返回文件路径,如不存在返回默认图片路径*/
    public String upload() {
        if (upload == null || upload.length() > 1024 * 1024 * 2) return WebConfig.defaultPic;
        String newFileName = new Date().getTime() + uploadFileName.substring(uploadFileName.indexOf("."));
        String dstPath = ServletActionContext.getServletContext().getRealPath("upload") + "\\" + newFileName;
        File dstFile = new File(dstPath);
        return copy(upload, dstFile) ? WebConfig.uploadDir + "/" + newFileName : WebConfig.defaultPic;
    }

    public String exception(String message, String path) {
        setMessage(message);
        setPath(path);
        return "succeed";
    }

    private static Boolean copy(File src, File dst) {
        try {
            Files.copy(src.toPath(), dst.toPath());
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    /*获得分页起止位置*/
    public int[] getP() {
        int p = page < 1 ? 1 : page;
        int start = (p - 1) * 20;
        int length = start + 20;
        return new int[]{start, length+1};
    }
    /*向页面写入分页链接*/
    public void getPLink(int count) {
        HttpServletRequest request=ServletActionContext.getRequest();
        String url=request.getRequestURL().toString();
        try {
            URIBuilder uriBuilder=new URIBuilder(url);
            page=page==0?1:page;
            String p = page <= 1 ? "#" : uriBuilder.addParameter("page",""+(page-1)).toString();
            String n= count<20 ? "#" : uriBuilder.addParameter("page",""+(page+1)).toString();
            Map req=getRequest();
            if(p.equals("#")&&n.equals("#")){
                req.put("page",false);
            }else {
                req.put("page",true);
                req.put("p",p);/*前一页链接*/
                req.put("n",n);/*后一页链接*/
            }
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public DB getDb() {
        return db;
    }

    public void setDb(DB db) {
        this.db = db;
    }
}
